﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Triples.Model {


    [Table]
    public class LocalHighscoreItem : INotifyPropertyChanged, INotifyPropertyChanging {
        public event PropertyChangedEventHandler PropertyChanged;
        public event PropertyChangingEventHandler PropertyChanging;

        private int _Id;
        private int _Rank;
        private string _Name;
        private int _Score;


        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int Id {
            get { return _Id; }
            set { Set(ref _Id, value, "Id"); }
        }


        public int Rank {
            get { return _Rank; }
            set { Set(ref _Rank, value, "Rank"); }
        }

        
        [Column]
        public string Name {
            get { return _Name; }
            set { Set(ref _Name, value, "Name"); }
        }


        [Column]
        public int Score {
            get { return _Score; }
            set { Set(ref _Score, value, "Score"); }
        }


        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName) {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName) {
            if (PropertyChanging != null) {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }


        private void Set<T>(ref T var, T val, string param) {
            if ((null == var && null != val) || !var.Equals (val)) {
                NotifyPropertyChanging(param);
                var = val;
                NotifyPropertyChanged(param);
            }
        }
    }
}
